class bi_bi:
    def __init__(self, bi,save_keys=[]):
        self.bi = bi
        self.fx = None
        self.pretimekey = None
        self.up_bis=[]#存放无限
        self.down_bis=[]#存放无限
        self.current_bi = None
        self.save_keys = save_keys
    # def get_curr_r(self):

    def oncalc(self,b):
        timekey=self.bi.current_bi["timekey"]
        if self.pretimekey !=timekey:
            # if self.fx>0:#上涨
            if self.bi.current_bi["flag"] == "max":
                if self.down_bis:#后续
                    maxprice=self.down_bis[-1][0]["price"]
                    if maxprice>self.bi.current_bi["price"]:#高位反转
                        self.fx = -2
                        bi_max = {"timekey": timekey, "price": maxprice, "flag": "min",
                                  "confirm_timkey": b["timekey"], "confirm_price": b["close"]}
                        if self.save_keys:
                            for i in self.save_keys:
                                bi_max[i] = b[i]
                        if self.current_bi:
                            self.up_bis.append((self.current_bi, bi_max))
                            if self.iscut:
                                while len(self.down_bis) > self.work_num:
                                    self.down_bis.pop(0)
                        self.current_bi = bi_max
                    else:#正常上涨
                        self.fx=1
                else:#初始
                    self.fx=1
            else:
                if self.up_bis:
                    minprice=self.up_bis[-1][0]["price"]
                    if minprice<self.bi.current_bi["price"]:#反转
                        self.fx = 2
                        bi_min = {"timekey": timekey, "price": minprice, "flag": "min",
                                  "confirm_timkey": b["timekey"], "confirm_price": b["close"]}
                        if self.save_keys:
                            for i in self.save_keys:
                                bi_min[i] = b[i]
                        if self.current_bi:
                            self.down_bis.append((self.current_bi, bi_min))
                        self.current_bi = bi_min
                    else:#正常上涨
                        self.fx=-1
                else:
                    self.fx=-1
            # else:#下跌
        self.pretimekey = timekey
        return self.fx